#include <QPalette>
#include "login.h"
#include "ui_login.h"
#include "account.h"

Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::Window);

    UsersDB *userdb = new UsersDB();
    QList<QString> usersNameList;
    usersNameList = userdb->getUsersName();

    ui->comboBox->addItems(usersNameList);

    //设置背景图片
    QPalette palette = this->palette();
    palette.setBrush(this->backgroundRole(),QBrush(QPixmap(":/resources/blue.jpg")));
    this->setPalette(palette);
}

Login::~Login()
{
    delete ui;
}

/**
 * @brief 显示登录界面
 *
 */
void Login::loginShow()
{
    UsersDB *userdb = new UsersDB();
    QList<QString> usersNameList;
    usersNameList = userdb->getUsersName();

    if (usersNameList.isEmpty())
    {
        ui->label_3->setText("请先创建玩家\n否则不能进入游戏~~");
        this->show();
        return;
    }
    ui->comboBox->clear();
    ui->comboBox->addItems(usersNameList);

    ui->label_3->setText(ui->comboBox->currentText() + "最高得分为: "
                         + userdb->getUsersMaxScore(ui->comboBox->currentText()));
    //设置事例字体点阵
    QFont font;
    int fontsize = userdb->getFontSize(ui->comboBox->currentText());
    font.setPointSize(fontsize);
    qApp->setFont(font);

    this->show();
}

/**
 * @brief 新建玩家
 *
 */
void Login::on_pushButton_clicked()
{
    this->hide();
    emit createNewPlayer(ui->comboBox->currentText());
}

/**
 * @brief 进入游戏
 *
 */
void Login::on_pushButton_2_clicked()
{
    if ( ui->comboBox->currentText().isEmpty())
    {
        ui->label_3->setText("请先创建玩家\n否则不能进入游戏~~");
        return;
    }

    UsersDB *userdb = new UsersDB();
    userdb->updateIsUsing(ui->comboBox->currentText());

    this->hide();
    emit enterGame();
}

/**
 * @brief 选择的玩家姓名改变时处理
 *
 * @param &name
 */
void Login::on_comboBox_currentTextChanged(const QString &name)
{
    UsersDB *userdb = new UsersDB();
    ui->label_3->setText(name + "最高得分为: "
                         + userdb->getUsersMaxScore(name));
}

/**
 * @brief 选择不同玩家,加载不同字体参数
 *
 * @param &username
 */
void Login::on_comboBox_currentIndexChanged(const QString &username)
{
    if (username.isEmpty())
        return;
    UsersDB *userdb = new UsersDB();
    //设置事例字体点阵
    QFont font;
    int fontsize = userdb->getFontSize(username);
    font.setPointSize(fontsize);
    qApp->setFont(font);
}
